package com.imjohn.controller;

import com.imjohn.entity.Comment;
import com.imjohn.entity.Comment_mid;
import com.imjohn.service.AticleService;
import com.imjohn.service.CommentService;
import com.imjohn.service.CommentmidService;
import com.imjohn.util.respUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api")
public class CommentController {
    @Autowired
    private CommentService commentService;
    @Autowired
    private AticleService aticleService;
    @Autowired
    private CommentmidService commentmidService;
    /**
     * 插入评论*/
    @RequestMapping("/AddComment")
    public Map AddComment( HttpServletRequest request, @RequestBody Comment comment) {
        HttpSession session = request.getSession();
        String userId = (String) session.getAttribute("userId");
        String userName = (String) session.getAttribute("userName");
        comment.setAnswerId(userId);
        comment.setAnswerName(userName);
       int i = commentService.AddComment(comment);
       int j = aticleService.subCommentNum(Integer.parseInt(comment.getAticleId()));
        if (i == 1 && j == 1) {
            Map map = new HashMap();
            map.put("msg", "success");
            return respUtil.respSuccessHash(map);
        } else {
            String fReason = "评论失败!";
            return respUtil.respFailer(fReason);
        }
    }
    /*点赞加1*/
    @RequestMapping("/addCommentGoodNum")
    public Map addCommentGoodNum(HttpServletRequest request, @RequestBody Comment_mid comment_mid){
        HttpSession session = request.getSession();
        String userId = (String) session.getAttribute("userId");
        comment_mid.setUserId(userId);
        /**
         * 重复点赞校验
         * */
        List<HashMap> ress =  commentmidService.isCommentGoodFlag(userId, comment_mid.getCommentId());
        if (ress != null && ress.size() == 1) {
            /**
             * 更新文章点赞数量
             * */
            int i = commentService.subParseNum(Integer.parseInt(comment_mid.getCommentId()));
            int k = commentmidService.cancelCommentGoodFlag(comment_mid);
            if (k == 1 && i == 1) {
                Map res = new HashMap();
                res.put("msg", "取消点赞");
                return respUtil.respSuccessHash(res);
            } else {
                String fReason = "系统错误!";
                return respUtil.respFailer(fReason);
            }
        }
        /**
         *
         * 未点过赞，插表
         * */
        int j = commentmidService.setCommentGoodFlag(userId, comment_mid.getCommentId(), "1");
        /**
         * 更新文章点赞数量
         * */
        int i = commentService.addParseNum(Integer.parseInt(comment_mid.getCommentId()));
        if (i == 1 && j == 1) {
            Map res = new HashMap();
            res.put("msg", "点赞成功");
            return respUtil.respSuccessHash(res);
        } else {
            String fReason = "失败!";
            return respUtil.respFailer(fReason);
        }
    }
}
